package de.fhdw.seminar.xtext.validation;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.xtext.validation.Check;

import de.fhdw.seminar.xtext.umlDsl.UmlClass;
import de.fhdw.seminar.xtext.umlDsl.UmlDslPackage;
import de.fhdw.seminar.xtext.umlDsl.UmlModel;


public class UmlDslJavaValidator extends AbstractUmlDslJavaValidator {

	
	@Check
	public void checkCyclicTypeHierarchy(UmlClass entity) {
		ArrayList<UmlClass> children = new ArrayList<UmlClass>();
		UmlClass current = entity;
		while(current.getSuperType() != null) {
			children.add(current);
			if (children.contains(current.getSuperType())){
				error("No cylcles in TypeHierarchy allowed", UmlDslPackage.Literals.UML_CLASS__NAME);
				break;
			} 
			current = current.getSuperType();
		}
	}
	
	@Check
	public void checkUniqueClassNames(UmlModel model){
		Set<String> cNames = new HashSet<String>();
		for(UmlClass c: model.getClasses()){
			if(!cNames.add(c.getName().toLowerCase())){
				error("Class names must be unique", /*UmlDslPackage.Literals.UML_CLASS__NAME*/ null);
				break;
			}
		}
	}
	
	@Check
	public void checkUpperCaseClassName(UmlClass umlClass){
		if(umlClass.getName().length()>0 && !Character.isUpperCase(umlClass.getName().charAt(0))){
			warning("Fist character of a class name should be upper case", UmlDslPackage.Literals.UML_CLASS__NAME);
		}
	}

}